<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class news extends CI_Controller {
	
	function __construct()
	{
		//$this->load->helper('url');
	 if(!login_user()){
       show_jump('已退出登录', site_url('manage/login/index'));
       }
		parent::__construct(); 
//	parse_str($_SERVER['QUERY_STRING'], $_GET); //解析链接，直接放到GET中
		$this->load->model('manage/m_news');
		

	}
	function index(){
		if(!IS_POST){	
	    $this->load->library('pagination');
		$config['base_url'] = base_url().'index.php/manage/news/index/';		
		$config['total_rows'] =  $this->m_news->get_news_num();
		$config['per_page'] = '100'; 
        $config['full_tag_open'] = '<p>';   
        $config['full_tag_close'] = '</p>';          
    	$this->pagination->initialize($config); 	
		 //get results   
	    $data['results'] = $this->m_news->get_news($config['per_page'],$this->uri->segment(4));
	 
	    foreach ($data['results'] as $k=>$res){
	    	$type_info=$this->m_news->get_news_type_name($res->type_id);
	    	$data['results'][$k]->type_id=$type_info['type_name'];
	    }
	//  print_r($data['results']);
	    $data['url']=base_url().'index.php/manage/news/';  	                              			
		$this->load->view('manage/news_index',$data);  
		}else{
			 $ids=$_POST['ids'];
			 $ids=implode(',',$ids);
			 $ids=trim($ids,',');		
			 $success=$this->m_news->batch_delete_news($ids);	
	    	if($success){
    	 	 show_jump('删除成功',site_url('manage/news/index')) 	;  
    	    }else{   	 	
       	     show_jump('删除失败',site_url('manage/news/index')) 	;
    	    }
		}
	}	
	function add(){		
		 if(!IS_POST){	
		 	 $data['type_info']=$this->m_news->get_news_type();
		 	 $data['act']='add';	 	
		     $this->load->view('manage/news_form',$data);
		 }else{
		 	
		 	$type_name = trim($this->input->post('type_name'));
		    $title=$this->input->post('title');
		    $content=$this->input->post('content');//内容
		    $attachment_title=$this->input->post('attachment_title');//附件标题
		    $pai=$this->input->post('pai');//附件标题
             if($_FILES['attachment_file']['name']){//上传附件
		      $config['upload_path'] = './static/news/attachment_file';
			  $config['allowed_types'] = 'zip';
			  $config['max_size'] = '10000';	
			  $config['encrypt_name']=true;		  
			  $this->load->library('upload', $config);			 
			  if ( ! $this->upload->do_upload('attachment_file'))
			  {	   
			   show_jump('文件上传失败 '.$this->upload->display_errors(),site_url('manage/news/add')) 	;
			  } 
			  else
			  {
			   $attachment_file=$this->upload->data();
			   $file_name=$attachment_file['file_name'];
			
			  }
			 } 			 
			 $is_common=$_POST['is_common'];//如果为1，插入到公共信息中
			 $time=time();
		   $insert_news= $this->m_news->save_news($type_name,$title,$content,$attachment_title,$file_name,$is_common,$time,$pai);//保存新闻		   
		   if($insert_news){
    	      //保存html文件
			 $html_url="static/news/news_".$type_name.".".$insert_news.".html";
			 $this->save_editor_content($content,$html_url);
			 $this->m_news->update_html_url($html_url,$insert_news);
			 if($is_common){
			 	$strip_content=strip_tags($content);
			 	$message=array('title'=>$title,
			 	               'content'=>utf8Substr($strip_content,0,30),
			 	               'url'=> base_url().$html_url,
	                           'attachment_title'=>$attachment_title,
			 	               'attachment_file'=>base_url().'static/news/attachment_file/'.$file_name,
			 	               'add_time'=>$time,
			 	);
			 	$json_message=addslashes(json_encode($message));
			 	
			 	$add_message=$this->m_news->save_common_message($file_name,$json_message,$title);
			 	//echo $this->db->last_query();die();
			 	if(!$add_message){
			 		 show_jump('保存公共信息失败',site_url('manage/news/index'));  
			 	}
			 }		
			 $last_news_1= $this->m_news->get_last_news();
			 $this->save_json_file(json_encode($last_news_1),'static/last_news_1.json');
			 $news_list_id=$this->m_news->get_news_list($type_name);
			 $this->save_json_file(json_encode($news_list_id),'static/news_list_'.$type_name.'_1.json');
			 show_jump('新闻添加成功',site_url('manage/news/index'));
    	   }else{   	
    	  
       	     show_jump('新闻添加失败',site_url('manage/news/index')) ;
    	   }
		 }
		  
	}
	function edit(){
		 $id=$_GET['id'];
		 $news_info=$this->m_news->get_news_ById($id);   
	 if(!IS_POST){	        
	          $data['type_info']=$this->m_news->get_news_type();
	 	     $data['result']=$news_info;
	 	     $data['act']='edit';	 	
		     $this->load->view('manage/news_form',$data);
		 }else{	
		 //	print_r($_POST);die();			 	
		 	$type_name = trim($this->input->post('type_name'));
		    $title=$this->input->post('title');
		    $content=$this->input->post('content');//内容
			$pai=$this->input->post('pai');//内容
		    $attachment_title=$this->input->post('attachment_title');//附件标题		 		   
             if($_FILES['attachment_file']['name']){//上传附件
		      $config['upload_path'] = './static/news/attachment_file';
			  $config['allowed_types'] = 'zip';
			  $config['max_size'] = '10000';	
			  $config['encrypt_name']=true;		  
			  $this->load->library('upload', $config);			 
			  if ( ! $this->upload->do_upload('attachment_file')){	   
			   show_jump('文件上传失败 '.$this->upload->display_errors(),site_url('manage/news/add')) 	;
			  } 
			  else{
			   $attachment_file=$this->upload->data();
			   $file_name=$attachment_file['file_name'];
			
			  }
			 }else{
			 	$file_name=$news_info['attachment_file'];
			 }		 
			 //保存html文件	
			 $this->save_editor_content($content,$news_info['content_url']);	
			 $is_common=$_POST['is_common'];//如果为1，插入到公共信息中
			 if($is_common){
			 	if(!$news_info['is_common']){
			 		$strip_content=strip_tags($content);
			 		$message=array('title'=>$title,
			 	               'content'=>(utf8Substr($strip_content,0,30)),
			 	               'url'=> base_url().$news_info['content_url'],
	                           'attachment_title'=>$attachment_title,
			 	               'attachment_file'=>base_url().'static/news/attachment_file/'.$file_name,
			 	               'add_time'=>$news_info['add_time'],
			 		);
			 		$json_message=addslashes(json_encode($message));
			 		$add_message=$this->m_news->save_common_message($file_name,$json_message,$title);
			 	}
			 	if(!$add_message){
			 		 show_jump('保存公共信息失败',site_url('manage/news/index'));  
			 	}
			 }	
			 
		     $update_news= $this->m_news->update_news($type_name,$title,$content,$attachment_title,$file_name,$is_common,$id,$pai);//保存新闻		   
		     if($update_news){
			 $last_news_1= $this->m_news->get_last_news();
			 $this->save_json_file(json_encode($last_news_1),'static/last_news_1.json');
			 $news_list_id=$this->m_news->get_news_list($type_name);
			 $this->save_json_file(json_encode($news_list_id),'static/news_list_'.$type_name.'_1.json');
			 show_jump('新闻修改成功',site_url('manage/news/index')) 	;
    	   }else{   	
    	  
       	     show_jump('新闻修改失败',site_url('manage/news/index')) 	;
    	   }		  	
		  }
	//	debugit(1);
		
	}
    function delete_news(){
    	 $id=$_GET['id'];
    	 $news_info=$this->m_news->get_news_ById($id);   
    	 unlink($news_info['content_url']);
    	 unlink('static/news/attachment_file/'.$news_info['content_url']);
    	 $success= $this->m_news->delete_news($id);
    	 if($success){
    	 	 show_jump('删除成功',site_url('manage/news/index')) 	;  
    	 }else{   	 	
       	   show_jump('删除失败',site_url('manage/news/index')) 	;
    	 }
    	
    }
   function save_editor_content($editor_content, $file = "") {
	if (empty ($editor_content)) {
		return '';
	} else {
		$content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
			<html xmlns=\"http://www.w3.org/1999/xhtml\">
			<head>
			<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
			<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0\">
			<meta name=\"apple-mobile-web-app-capable\" content=\"yes\">
			<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\">
			<meta name=\"format-detection\" content=\"telephone=no\">
			<title></title>
			</head>
			<body>" . $editor_content . "</body></html>";

		//$content=str_replace("\\","",$content);
		$content = stripslashes($content);
		if ($file) {
			$file_name = $file;
		} else {
			$file_name = "static/news/" . date('YmdHis') . ".html";
		}
		$fp = fopen($file_name, "w+");
		fputs($fp, $content);
		fclose($fp);
		return $file_name;
	}
}
  function save_json_file($content,$file_name){
  	    $fp = fopen($file_name, "w+");
		fputs($fp, $content);
		fclose($fp);
  	
  }

}

?>